/*
 * @lc app=leetcode.cn id=91 lang=golang
 *
 * [91] 解码方法
 */

// @lc code=start

// 各种条件
func numDecodings(s string) int {
	n := len(s)
	if n == 0 {
		return 0
	}
	if s[0] == '0' {
		return 0
	}
	if n == 1 {
		return 1
	}
	dp := make([]int, n+1)

	dp[0] = 1
	dp[1] = 1

	for i := 2; i <= n; i++ {
		v := s[i-1]
		prev := s[i-2]
		if (prev == '0' || prev > '2') && v == '0' {
			return 0
		}
		// i = i+1
		if prev < '2' && prev > '0' || prev == '2' && v < '7' {
			if v == '0' {
				dp[i] = dp[i-2]
			} else {
				dp[i] = dp[i-1] + dp[i-2]
			}
		} else {
			dp[i] = dp[i-1]
		}
	}
	return dp[n]
}

// @lc code=end
